package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.User;
import ex.com.jm.food.service.AdminUserService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;
import ex.com.jm.food.utils.MD5Utils;

/**
 * 管理员管理用户
 * @author zhaojw
 *
 */
public class AdminUserAction extends ActionSupport {

	
	private static final long serialVersionUID = 1L;

	private AdminUserService adminUserService;
    private User user;
    private int initPage=1;
    private int pageRow=10;
    private String order;
    private String sort;
    private String userId;
    private String userName;
    private String userEmail;
    private String s_date;
    private String e_date;
    private Integer type;
    private int totalCount;
    private String province;
    private String citys;
    private String countys;
    private String streets;
    private Map<String, String> dateMap;
	public AdminUserService getAdminUserService() {
		return adminUserService;
	}

	public void setAdminUserService(AdminUserService adminUserService) {
		this.adminUserService = adminUserService;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public int getInitPage() {
		return initPage;
	}

	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}

	public int getPageRow() {
		return pageRow;
	}

	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public String getS_date() {
		return s_date;
	}

	public void setS_date(String s_date) {
		this.s_date = s_date;
	}

	public String getE_date() {
		return e_date;
	}

	public void setE_date(String e_date) {
		this.e_date = e_date;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCitys() {
		return citys;
	}

	public void setCitys(String citys) {
		this.citys = citys;
	}

	public String getCountys() {
		return countys;
	}

	public void setCountys(String countys) {
		this.countys = countys;
	}

	public String getStreets() {
		return streets;
	}

	public void setStreets(String streets) {
		this.streets = streets;
	}

	public Map<String, String> getDateMap() {
		return dateMap;
	}

	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}

	public String userList(){
		dateMap=DateUtils.getDate();
		return "userList";
	}
	
	public void getUsers(){
		int offset=(initPage-1)*pageRow;
		List<User> users=this.getAdminUserService().getUsers(offset, pageRow, userName, s_date, e_date, type, sort, order);
		 totalCount=this.getAdminUserService().userCount(userName, s_date, e_date, type);
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8"); 
		 JSONArray userList=JSONArray.fromObject(users);
		 JSONObject jo=null;
		 if(totalCount!=0){
		   jo=JsonUtils.buildJson(userList, initPage, pageRow, totalCount);
		 }
		 PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print(jo);
	}
	
	public String editUser(){
		if(userId!=null){
			user=this.getAdminUserService().getUser(userId);
			String[] address=user.getUserAddress().split(";");
			province=address[0];
			citys=address[1];
			countys=address[2];
			streets=address[3];
		}
		System.out.println(province+""+citys+";"+countys+";"+streets);
		return "edit";
	}
	
	public String saveUser(){
		if(user.getUserEmail()==null||user.getUserEmail().equals("")){
			addFieldError("user.userEmail","用户邮箱不能为空");
			return "edit";
		}else if (user.getUserName()==null||user.getUserName().equals("")) {
			addFieldError("user.userName","用户名称不能为空");
			return "edit";
		}else if (user.getUserPwd()==null||user.getUserPwd().equals("")) {
			addFieldError("user.userPwd","用户密码不能为空");
			return "edit";
		}else if (user.getUserPwd().length()<6||user.getUserPwd().length()>9) {
			addFieldError("user.userPwd","用户密码长度为6到9位");
			return "edit";
		}else if (user.getUserPhone()==null||user.getUserPhone().equals("")) {
			addFieldError("user.userPhone","用户联系方式不能为空");
			return "edit";
		}else if (user.getRealName()==null||user.getRealName().equals("")) {
			addFieldError("user.realName","用户真实姓名不能为空");
			return "edit";
		}
		User checkName=this.getAdminUserService().checkName(user.getUserName());
		User checkEmail=this.getAdminUserService().checkEmail(user.getUserEmail());
		if(checkName!=null&&user.getUserId()==null){
			addFieldError("user.userName","用户名称已被注册");
			return "edit";
		}
		if(checkEmail!=null&&user.getUserId()==null){
			addFieldError("user.userEmail","用户邮箱已被注册");
			return "edit";
		}
		String address=province+";"+citys+";"+countys+";"+streets;
		if(address==";;;"){
			addFieldError("user.userAddress","用户联系方式不能为空");
			return "edit";
		}
		user.setUserAddress(address);
		if (user.getUserId()==null||user.getUserId().equals("")) {
			user.setType(0);
			System.err.println(user.getType());
			user.setCreated(DateUtils.getCurrentDate());
		}
		System.err.println(user.getType());
		user.setUserPwd(MD5Utils.GetMD5Code(user.getUserPwd()));
		if(user.getUserId()!=null&&!user.getUserId().equals("")){
			this.getAdminUserService().updateUser(user);
			return "userList";
		}
		
		this.getAdminUserService().saveUser(user);
		return "userList";
		
	}
	
	public void checkName(){
		HttpServletResponse response = ServletActionContext.getResponse();  
	    response.setContentType("text/plain;charset=utf-8");  
		User userInfo=this.getAdminUserService().checkName(userName);
		String msg;
		if(userInfo!=null&&userId==null){
			msg="该用户名称已被注册！";;
		}else {
			msg="该用户名称可用！";
		}
		JSONArray jsonMsg=JsonUtils.buildMessage(msg);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jsonMsg);
	    out.print(jsonMsg);
	}
	
	public void checkEmail(){
		HttpServletResponse response = ServletActionContext.getResponse();  
	    response.setContentType("text/plain;charset=utf-8");  
		User userInfo=this.getAdminUserService().checkName(userEmail);
		String msg;
		if(userInfo!=null&&userId==null){
			msg="该邮箱已被注册！";;
		}else {
			msg="该邮箱可用！";
		}
		JSONArray jsonMsg=JsonUtils.buildMessage(msg);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jsonMsg);
	    out.print(jsonMsg);
	}
	
	public String updateType(){
		boolean flag=this.getAdminUserService().updateType(type, userId);
		if(flag==true){
		return "userList";
		}else{
			return "updateFail";
		}
	}
	
	public String delUser(){
		this.getAdminUserService().delUser(userId);
		return "userList";
	}
}
